<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\portal_promotion\controller; //Demo插件英文名，改成你的插件英文就行了

use cmf\controller\PluginAdminBaseController;
use think\Db;
use app\portal\model\PortalPostModel;
use app\portal\model\PortalCategoryModel;
use plugins\portal_promotion\model\PortalPromotionModel;

use app\portal\service\PostService;

class AdminIndexController extends PluginAdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    /**
     * 文章扩展
     * @adminMenu(
     *     'name'   => '文章扩展',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '文章扩展字段插件列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {

        $param = $this->request->param();

        $categoryId = $this->request->param('category', 0, 'intval');
        
        $filter = $param;

        $where = [
            'a.create_time' => ['>=', 0],
            'a.delete_time' => 0
        ];

        $join = [
            ['__USER__ u', 'a.user_id = u.id'],
            ['__PORTAL_PROMOTION__ pr', 'pr.post_id = a.id']
        ];

        $field = 'a.*,pr.id as promotion_id,pr.end_date,u.user_login,u.user_nickname,u.user_email';

        $category = empty($filter['category']) ? 0 : intval($filter['category']);
        if (!empty($category)) {
            $where['b.category_id'] = ['eq', $category];
            array_push($join, [
                '__PORTAL_CATEGORY_POST__ b', 'a.id = b.post_id'
            ]);
            $field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,pr.end_date,u.user_login,u.user_nickname,u.user_email';
        }

        $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
        if (!empty($keyword)) {
            $where['a.post_title'] = ['like', "%$keyword%"];
        }
       
        $where['a.post_type'] = 1;
        
        $portalPostModel = new PortalPostModel();
        $data        = $portalPostModel->alias('a')->field($field)
            ->join($join)
            ->where($where)
            ->order('update_time', 'DESC')
            ->paginate(10);

        $data->appends($param);

        $portalCategoryModel = new PortalCategoryModel();
        $categoryTree        = $portalCategoryModel->adminCategoryTree($categoryId);

        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        $this->assign('keyword', isset($param['keyword']) ? $param['keyword'] : '');
        $this->assign('articles', $data->items());
        $this->assign('category_tree', $categoryTree);
        $this->assign('category', $categoryId);
        $this->assign('page', $data->render());


        return $this->fetch('/admin_index');
    }



    /**
     * 编辑文章
     * @adminMenu(
     *     'name'   => '编辑扩展',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑文章扩展字段',
     *     'param'  => ''
     * )
     */
    public function edit()
    {
        
        $id = $this->request->param('id', 0, 'intval');
        
        $portalPromotionModel = new PortalPromotionModel();
        $promotion            = $portalPromotionModel->find($id);
        
        $this->assign('promotion', $promotion);
    
        return $this->fetch('/admin_edit');
    }
 
     /**
      * 编辑扩展提交
      * @adminMenu(
      *     'name'   => '编辑扩展提交',
      *     'parent' => 'index',
      *     'display'=> false,
      *     'hasView'=> false,
      *     'order'  => 10000,
      *     'icon'   => '',
      *     'remark' => '编辑文章提交',
      *     'param'  => ''
      * )
      */
    public function editPost($id)
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();

            $promotion   = $data['promotion'];
            //$result = $this->validate($post, 'AdminArticle');
      
            /*
            if ($result !== true) {
                $this->error($result); 
            }*/

            $portalPromotionModel = new PortalPromotionModel();

            $promotionObj            = $portalPromotionModel->find($id);
            $promotionObj->end_date  = $promotion['end_date'];
            
            $promotionObj->save();

            $this->success('保存成功!');

        }
    }

   
}
